package com.sinopec.basemodule.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.sinopec.basemodule.AppConstants;
import com.sinopec.basemodule.R;
import com.sinopec.basemodule.common.Logger;

import java.security.MessageDigest;

public class ShowImageUtils {
    /**
     * (1)
     * 显示图片Imageview
     *
     * @param context   上下文
     * @param errorImg  错误的资源图片
     * @param url       图片链接
     * @param imageView 组件
     */
    public static void showImageView(Context context, int errorImg, String url,
                                     ImageView imageView) {
        if (!StringUtils.isUrl(url)) {
            url = AppConstants.IMAGE_SEVER + url;
        }
        RequestOptions requestOption = new RequestOptions();
        requestOption.centerCrop();
        requestOption.error(errorImg);
        Glide.with(context).load(url)// 加载图片
                //.apply(requestOption)// 设置错误图片
                .centerCrop()
                .error(errorImg)
                .into(imageView);
    }

    public static void showImageViewWithCacheRrrorImg(Context context, int errorImg, String url,
                                                      ImageView imageView) {
        RequestOptions requestOption = new RequestOptions();
        requestOption.centerCrop();
        requestOption.error(errorImg);
        requestOption.diskCacheStrategy(DiskCacheStrategy.RESOURCE).skipMemoryCache(false).dontAnimate();
        Glide.with(context).load(url)// 加载图片
                .apply(requestOption)// 设置错误图片
                .into(imageView);
    }

    public static void showImageView(Context context, String url,
                                     ImageView imageView) {
        if (!StringUtils.isUrl(url)) {
            url = AppConstants.IMAGE_SEVER + url;
        }
        Logger.d("显示图片地址：" + url);
        Glide.with(context).load(url)// 加载图片
                .apply(new RequestOptions()
                                .centerCrop()
//                        .diskCacheStrategy(DiskCacheStrategy.NONE)
//                        .skipMemoryCache(true)
                )
                .into(imageView);
    }

    public static void showImageCircle(Context context,int errorImg, String url,
                                     ImageView imageView) {
        if (!StringUtils.isUrl(url)) {
            url = AppConstants.IMAGE_SEVER + url;
        }
        Logger.d("显示图片地址：" + url);
        RequestOptions requestOption = new RequestOptions();
        requestOption.centerCrop();
        requestOption.error(errorImg);
        requestOption.centerCrop();
        Glide.with(context).load(url)// 加载图片
                .apply(requestOption)
                .into(imageView);
    }

    /**
     * 加载失败展示
     *
     * @param context
     * @param url
     * @param imageView
     * @param loadImageListener
     */
    public static void showLocalImage(Context context, String url,
                                      ImageView imageView, RequestListener loadImageListener) {
        RequestOptions requestOption = new RequestOptions();
        requestOption.error(R.drawable.wutu);
        Glide.with(context).load(url)// 加载图片
                .apply(requestOption)// 设置错误图片
                .listener(loadImageListener)
                .into(imageView);
    }

    /**
     * 加载失败展示
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void showLocalImage(Context context, String url,
                                      ImageView imageView) {
        RequestOptions requestOption = new RequestOptions();
        requestOption.error(R.drawable.wutu).centerCrop();
        Glide.with(context).load(url)// 加载图片
                .apply(requestOption)// 设置错误图片
                .into(imageView);
    }


    public static void showImageView(Context context, int errorImg, String url,
                                     ImageView imageView, RequestListener loadImageListener) {
        url = AppConstants.IMAGE_SEVER + url;
        RequestOptions requestOption = new RequestOptions();
        requestOption.error(errorImg).centerCrop();
        Glide.with(context)
                .load(url)
                .listener(loadImageListener)
                .apply(requestOption)
                .into(imageView);
    }

    public static void showImageView(Context context, String url,
                                     ImageView imageView, RequestListener loadImageListener) {
        url = AppConstants.IMAGE_SEVER + url;
        RequestOptions requestOption = new RequestOptions();
        requestOption.centerCrop();
//        requestOption.error(R.drawable.wutu);
        Glide.with(context)
                .load(url)
                .apply(requestOption)
                .listener(loadImageListener)
                .into(imageView);
    }

    public static void showImageWithMemoryCache(Context context, String url,
                                                ImageView imageView) {
        if (!StringUtils.isUrl(url)) {
            url = AppConstants.IMAGE_SEVER + url;
        }
        Logger.d("显示图片地址：" + url);
        Glide.with(context).load(url)// 加载图片
                .apply(new RequestOptions()
                        .centerCrop()
                        .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                        .skipMemoryCache(false)
                )
                .into(imageView);
    }

    /**
     * @author lwj
     * @Description: 显示圆角图片
     * @date 2018/9/28 11:18
     */
    public static void showRoundImageWithMemoryCache(Context context, String url,
                                                     ImageView imageView,int dp) {
        if (!StringUtils.isUrl(url)) {
            url = AppConstants.IMAGE_SEVER + url;
        }
        Glide.with(context).load(url)// 加载图片
                .apply(new RequestOptions()
                        .error(R.drawable.error)
                        .centerCrop()
                        .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                        .skipMemoryCache(false)
                        .transform(new GlideRoundTransform(context,dp))
                )
                .into(imageView);
    }


    public static void showImageViewWithoutCache(Context context, String url,
                                                 ImageView imageView, RequestListener loadImageListener) {
        RequestOptions requestOptions = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .override(1080, 1920);

        url = AppConstants.IMAGE_SEVER + url;

        Glide.with(context)
                .load(url)
                .apply(requestOptions)
                .listener(loadImageListener)
                .into(imageView);
    }


    /**
     * @author lwj
     * @ClassName ShowImageUtils
     * @Description: 圆角
     * @date 2018/9/28 11:17
     */
    public static class GlideRoundTransform extends BitmapTransformation {

        private static float radius = 0f;

        public GlideRoundTransform(Context context) {
            this(context, 4);
        }

        public GlideRoundTransform(Context context, int dp) {
            super();
            this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
        }

        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
            return roundCrop(pool, bitmap);
        }

        private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
            if (source == null) return null;

            Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
            if (result == null) {
                result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
            }

            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);
            RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, paint);
            return result;
        }

        public String getId() {
            return getClass().getName() + Math.round(radius);
        }

        @Override
        public void updateDiskCacheKey(MessageDigest messageDigest) {

        }

    }

}
